// src/main/java/cn/edu/cdu/health/service/impl/DepartmentServiceImpl.java
package cn.edu.cdu.health.service.impl;

import cn.edu.cdu.health.entity.Department;
import cn.edu.cdu.health.mapper.DepartmentMapper;
import cn.edu.cdu.health.service.IDepartmentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Slf4j
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Override
    public Department getByDeptCode(String deptCode) {
        if (!StringUtils.hasText(deptCode)) {
            throw new IllegalArgumentException("科室编码不能为空");
        }

        return this.lambdaQuery()
                .eq(Department::getDeptCode, deptCode)
                .eq(Department::getIsDeleted, 0)
                .one();
    }

    @Override
    public IPage<Department> getDepartmentPage(int current, int size, String keyword) {
        Page<Department> page = new Page<>(current, size);
        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(Department::getIsDeleted, 0)
                .orderByAsc(Department::getSortOrder)
                .orderByDesc(Department::getCreateTime);

        if (StringUtils.hasText(keyword)) {
            wrapper.and(w -> w
                    .like(Department::getDeptCode, keyword)
                    .or()
                    .like(Department::getDeptName, keyword)
                    .or()
                    .like(Department::getDeptHead, keyword)
            );
        }

        return this.page(page, wrapper);
    }

    @Override
    public List<Department> getActiveDepartments() {
        return this.lambdaQuery()
                .eq(Department::getStatus, 1)
                .eq(Department::getIsDeleted, 0)
                .orderByAsc(Department::getSortOrder)
                .orderByAsc(Department::getDeptName)
                .list();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(Long id, Integer status) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("科室ID不能为空");
        }

        if (status == null || (status != 0 && status != 1)) {
            throw new IllegalArgumentException("状态值必须是0或1");
        }

        Department department = new Department();
        department.setId(id);
        department.setStatus(status);

        return this.updateById(department);
    }

    @Override
    public boolean isDeptCodeExists(String deptCode) {
        if (!StringUtils.hasText(deptCode)) {
            return false;
        }

        Long count = this.lambdaQuery()
                .eq(Department::getDeptCode, deptCode)
                .eq(Department::getIsDeleted, 0)
                .count();

        return count > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean logicDelete(Long id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("科室ID不能为空");
        }

        Department department = new Department();
        department.setId(id);
        department.setIsDeleted(1);

        return this.updateById(department);
    }
}